home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <string.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <math.h>
- #include <stdlib.h>
- #include "tk.h"
-
-
- GLenum doubleBuffer, directRender;
- int winW = 300, winH = 300;
-
- char *earthTexFileName = 0, *skyTexFileName = 0;
- TK_RGBImageRec *earthImage, *skyImage;
- GLint skyList, earthList;
-
- float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
- float decal[] = {GL_DECAL};
- float modulate[] = {GL_MODULATE};
- float repeat[] = {GL_REPEAT};
- float clamp[] = {GL_CLAMP};
- float nr[] = {GL_NEAREST};
- float ln[] = {GL_LINEAR};
- float nr_mipmap_nr[] = {GL_NEAREST_MIPMAP_NEAREST};
- float nr_mipmap_ln[] = {GL_NEAREST_MIPMAP_LINEAR};
- float ln_mipmap_nr[] = {GL_LINEAR_MIPMAP_NEAREST};
- float ln_mipmap_ln[] = {GL_LINEAR_MIPMAP_LINEAR};
-
- int horizon;
- float texMinX, texMinY, texMaxX, texMaxY;
-
-
- static void Init(void)
- {
-
- earthImage = tkRGBImageLoad(earthTexFileName);
- skyImage = tkRGBImageLoad(skyTexFileName);
-
- glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
-
- magFilter = nr;
- minFilter = nr;
- sWrapMode = repeat;
- tWrapMode = repeat;
-
- glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, decal);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
- glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
- glEnable(GL_TEXTURE_2D);
-
- glClearColor(0.0, 0.0, 0.0, 0.0);
-
- horizon = winH / 2;
-
- texMinX = 0.25;
- texMaxX = 0.75;
-
- texMinY = 0.25;
- texMaxY = 0.75;
-
- skyList = glGenLists(1);
- glNewList(skyList, GL_COMPILE);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, skyImage->sizeX, skyImage->sizeY,
- GL_RGB, GL_UNSIGNED_BYTE, skyImage->data);
- glEndList();
-
- earthList = glGenLists(1);
- glNewList(earthList, GL_COMPILE);
- gluBuild2DMipmaps(GL_TEXTURE_2D, 3, earthImage->sizeX, earthImage->sizeY,
- GL_RGB, GL_UNSIGNED_BYTE, earthImage->data);
- glEndList();
- }
-
- static void Reshape(int width, int height)
- {
-
- winW = width;
- winH = height;
-
- glViewport(0, 0, (GLint)winW, (GLint)winH);
-
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluOrtho2D(0, winW, 0, winH);
- glMatrixMode(GL_MODELVIEW);
- }
-
- static GLenum Key(int key, GLenum mask)
- {
-
- switch (key) {
- case TK_ESCAPE:
- tkQuit();
-
- case TK_LEFT:
- texMinX -= 5.0 / (float)winW;
- texMaxX -= 5.0 / (float)winW;
- break;
- case TK_RIGHT:
- texMinX += 5.0 / (float)winW;
- texMaxX += 5.0 / (float)winW;
- break;
-
- case TK_UP:
- texMinY += 5.0 / (float)winH;
- texMaxY += 5.0 / (float)winH;
- break;
- case TK_DOWN:
- texMinY -= 5.0 / (float)winH;
- texMaxY -= 5.0 / (float)winH;
- break;
-
- case TK_1:
- horizon -= 5;
- texMinY -= 5.0 / (float)winH;
- texMinY += 5.0 / (float)winH;
- texMaxY += 5.0 / (float)winH;
- break;
- case TK_2:
- horizon += 5;
- texMinY += 5.0 / (float)winH;
- texMinY -= 5.0 / (float)winH;
- texMaxY -= 5.0 / (float)winH;
- break;
-
- default:
- return GL_FALSE;
- }
- return GL_TRUE;
- }
-
- static void Draw(void)
- {
-
- glClear(GL_COLOR_BUFFER_BIT);
-
- glPushMatrix();
-
- glCallList(skyList);
- glBegin(GL_POLYGON);
- glTexCoord2f(texMinX, texMinY);
- glVertex2i(0, horizon);
- glTexCoord2f(texMaxX, texMinY);
- glVertex2i(winW, horizon);
- glTexCoord2f(texMaxX, texMaxY);
- glVertex2i(winW, winH);
- glTexCoord2f(texMinX, texMaxY);
- glVertex2i(0, winH);
- glEnd();
-
- glCallList(earthList);
- glBegin(GL_POLYGON);
- glTexCoord2f(0.0, 0.0);
- glVertex2i(0, 0);
- glTexCoord2f(1.0, 0.0);
- glVertex2i(winW, 0);
- glTexCoord2f(1.0, 1.0);
- glVertex2i(winW, horizon);
- glTexCoord2f(0.0, 1.0);
- glVertex2i(0, horizon);
- glEnd();
-
- glPopMatrix();
-
- glFlush();
-
- if (doubleBuffer) {
- tkSwapBuffers();
- }
- }
-
- static GLenum Args(int argc, char **argv)
- {
- GLint i;
-
- doubleBuffer = GL_FALSE;
- directRender = GL_TRUE;
-
- for (i = 1; i < argc; i++) {
- if (strcmp(argv[i], "-sb") == 0) {
- doubleBuffer = GL_FALSE;
- } else if (strcmp(argv[i], "-db") == 0) {
- doubleBuffer = GL_TRUE;
- } else if (strcmp(argv[i], "-dr") == 0) {
- directRender = GL_TRUE;
- } else if (strcmp(argv[i], "-ir") == 0) {
- directRender = GL_FALSE;
- } else if (strcmp(argv[i], "-earth") == 0) {
- if (i+1 >= argc || argv[i+1][0] == '-') {
- printf("-earth (No file name).\n");
- return GL_FALSE;
- } else {
- earthTexFileName = argv[++i];
- }
- } else if (strcmp(argv[i], "-sky") == 0) {
- if (i+1 >= argc || argv[i+1][0] == '-') {
- printf("-sky (No file name).\n");
- return GL_FALSE;
- } else {
- skyTexFileName = argv[++i];
- }
- } else {
- printf("%s (Bad option).\n", argv[i]);
- return GL_FALSE;
- }
- }
- return GL_TRUE;
- }
-
- void main(int argc, char **argv)
- {
- GLenum type;
-
- if (Args(argc, argv) == GL_FALSE) {
- tkQuit();
- }
-
- tkInitPosition(0, 0, winW, winH);
-
- type = TK_RGB;
- type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
- type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
- tkInitDisplayMode(type);
-
- if (tkInitWindow("Two Texture Test") == GL_FALSE) {
- tkQuit();
- }
-
- Init();
-
- tkExposeFunc(Reshape);
- tkReshapeFunc(Reshape);
- tkKeyDownFunc(Key);
- tkDisplayFunc(Draw);
- tkExec();
- }
-